/* eslint-disable */
var path = require('path');

const LARAVEL_DIR = path.resolve( __dirname , '../../public/web-admin/' );

require('shelljs/global'); // node 可以这样引入全局变量 ? 用于调用 shell 命令
env.NODE_ENV = 'production'; 
var ora = require('ora');   // 一个 spinner 的动画
// 载入 webpack( webpackConfig , callback ) 执行编译
var webpack = require('webpack');  
var webpackConfig = require('./webpack.prod.config.js');  

console.log(' [WARNING] Production Building !!!! \n' );

var spinner = ora('building for production...');
spinner.start();

// 清理上次编译的内容, 然后复制 static 静态资源
rm('-rf', LARAVEL_DIR);

// returns a Compiler instance
var compiler = webpack( webpackConfig );

compiler.plugin( "compile" , beforeCompile );

compiler.watch({ // watch options:
    aggregateTimeout: 300, // wait so long for more changes
    poll: true ,// use polling instead of native watchers
    timings : true // 显示编译时间?
}, afterCompiled );

function beforeCompile() {
    console.log( '\n开始编译: ' + getNowTime() );
}

var compile_count = 1;

function afterCompiled (err, stats) {
    spinner.stop();
    if (err) throw err;

    process.stdout.write(stats.toString({
            colors: true,
            modules: false,
            children: false,
            chunks: false,
            chunkModules: false
        }) + '\n');

    console.log( '编译次数 : ' + compile_count++ );
    console.log( 'now : ' + getNowTime() ) ;
}

function getNowTime(){
    return (new Date).toTimeString().split(" ").shift();
}
